package tech.com.commoncore.others;

import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import tech.com.commoncore.R;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.ApplicationUtil;

/**
 * Desc: 用户协议说明.
 */
@Route( path = "/core/useragree")
public class UserAgreementActivity extends BaseTitleActivity {
    private TextView mTvContent;
    public static final int TYPE_USER_AGREE = 0;  //注册协议
    public static final int TYPE_USER_PRIVATE = 1;  //带应用,隐私
    public static final int TYPE_USER_PRIVATE_PLATFORM = 11;  //带公司的,隐私

    public String companyName=null;
    int type;
    @Override
    public void setTitleBar(TitleBarView titleBar) {
        try {
            type = getIntent().getIntExtra("type",0);
            companyName = getIntent().getStringExtra("compony");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(type == TYPE_USER_AGREE){
            titleBar.setTitleMainText("用户协议");
        }else{
            titleBar.setTitleMainText("隐私协议");
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_user_agreement;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        mTvContent =  findViewById(R.id.tv_content);
        String text = null ;
        if(type == TYPE_USER_PRIVATE_PLATFORM){
            text = getFromAssets("RegistAndProtect.html");
            text = text.replaceAll("MT4期货原油软件", companyName == null? ApplicationUtil.getAppName(mContext) :companyName);
        }else if(type == TYPE_USER_PRIVATE){
            text = getFromAssets("private.html");
            text = text.replaceAll("MT4期货原油软件", ApplicationUtil.getAppName(mContext));
        }else{
            text = getFromAssets("RegistAndProtect.html");
            text = text.replaceAll("MT4期货原油软件", ApplicationUtil.getAppName(mContext));
        }

        mTvContent.setText(Html.fromHtml(text));
    }

    public String getFromAssets(String fileName) {
        try {
            InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
            return Result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "解析错误";
    }

}
